const util = require('../../utils/util.js')

Component({
	/**
	 * 组件的属性列表
	 */
	options: {
		multipleSlots: true, // 在组件定义时的选项中启用多slot支持
	},
	behaviors: ['wx://form-field'],
	/**
	 * 页面传参
	 */
	properties: {
		form_info: {
			type: Object,
			value: {}
		},
		task_month: {
			type: String,
			value: {}	
		}
	},
	observers: {
		'form_info'(form_info) {
			//console.log(form_info)
			var that = this
			that.setData({
				lists: form_info,
			})
			if (form_info.value) {
				that.setData({
					val: form_info.value,
					value: form_info.value,
					defaultDate: new Date(form_info.value).getTime()
				})
			}
		},
		'task_month'(task_month) {
			//console.log(task_month.substring(5, 7))
			var that = this
			that.setData({
				t_month: task_month,
			})
		}

	},
	/**
	 * 组件的初始数据
	 */
	data: {
		show: false,
		val: '',
		date: '',
		t_month: '',
		minDate: new Date().getTime(),
		maxDate: new Date(2021, 0, 31).getTime(),
		defaultDate: '',
		currentDate: new Date().getTime()
	},
	/**
	 * 组件周期
	 */
   lifetimes: {
	    // 进入节点树
        attached: function() {
			var task_month = this.data.t_month.substring(5,7)
            const date = new Date();
            //const nowYear = date.getFullYear()
			//console.log(date.getFullYear())
			const nowYear = this.data.t_month.substring(0,4)
			//console.log()
		    //const nowMonth = date.getMonth();
			const nowMonth = this.data.t_month.substring(5,7)
			//console.log()
            const nowDay = new Date(nowYear, nowMonth, 0).getDate()
			//nowDay = nowDay < 10 ? '0' + nowDay : nowDay
			
			
			const minDate = new Date(nowYear, nowMonth - 1, 1).getTime()
			const maxDate = new Date(parseInt(nowYear) + 1, nowMonth - 1, nowDay).getTime()
            this.setData({
				minDate: minDate,
                maxDate: maxDate
            })
        }
    },
	/**
	 * 组件的方法列表
	 */
	methods: {
		showPopup() {
			this.setData({
				show: true
			});
		},
		formatDate(date) {
			date = new Date(date);
			return `${date.getFullYear()}-${("0" + (date.getMonth() + 1)).slice(-2)}-${("0" + date.getDate()).slice(-2)} ${("0" + date.getHours()).slice(-2)}:${("0" + date.getMinutes()).slice(-2)}:${("0" + date.getSeconds()).slice(-2)}`;
		},
		onConfirm(event) {
			console.log(new Date(event.detail))
			this.setData({
				show: false,
				date: this.formatDate(event.detail),
				val: this.formatDate(event.detail),
				value: this.formatDate(event.detail)
			});
		},
		onCancel() {
			this.setData({
				show: false
			});
		},
		onClose() {
			this.setData({
				show: false
			});
		}
	}
})
